package medium;

import java.util.ArrayList;
import java.util.List;

/*
230. 二叉搜索树中第K小的元素
解题思路：中序遍历
 */
public class LeeCode230 {

  public static void main(String[] args) {

  }

  public int kthSmallest(TreeNode root, int k) {
    ArrayList<TreeNode> list = new ArrayList<>();
    inOrder(root,list);
    return list.get(k - 1).val;
  }

  public void inOrder(TreeNode root, List<TreeNode> list){
    if (root != null){
      inOrder(root.left,list);
      list.add(root);
      inOrder(root.right,list);
    }
  }

  public class TreeNode {
    int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
          this.left = left;
          this.right = right;
      }
 }
}
